﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegatesAndEvents
{
    class SequencesDelegate
    {
        public delegate double SequenceDelegate();

        static int Factorial(int number)
        {
            int result = 1;
            for (int i = 2; i <= number; i++)
            {
                result *= i;
            }
            return result;
        }
        public static double CalculateSequence1()
        {
            int x = 2;
            double sum = 0;
            double currentItem = 1;
            double nextItem = ((double)1 / (double)2); 
            while ((currentItem - nextItem) >= 0.01)
            {
                
                sum += currentItem + nextItem;
                x *= 2;
                currentItem = ((double)1 / x);
                nextItem = ((double)1 / (2*x));
                x *= 2;

            }
            Console.WriteLine(sum);
            return sum;
        }

        public static double CalculateSequence2()
        {
            int x = 3;
            double sum = 1;
            double currentItem = ((double)1 / 2);
            double nextItem = ((double)1 / (2 * 6));
            while ((currentItem - nextItem) > 0.01)
            {

                sum += currentItem + nextItem;
                x += 1;
                int p = Factorial(x);
                currentItem = ((double)1 / p);
                p = Factorial(x+1);
                nextItem = ((double)1 / p);
                x += 1;

            }
            Console.WriteLine(sum);
            return sum;
        }

        public static double CalculateSequence3()
        {
            int x = 2;
            int i = 1;
            double sum = 0;
            double currentItem = 1;
            double nextItem = ((double)1 / (double)2);
            while ((currentItem - nextItem) >= 0.01)
            {

                sum += currentItem + nextItem;
                x *= 2;
                currentItem = Math.Pow((-1), i)*((double)1 / x);
                i++;
                nextItem = Math.Pow((-1), i) * ((double)1 / (2 * x));
                x *= 2;
                i++;

            }
            Console.WriteLine(sum);
            return sum;
        }

       

       

    }
}
